import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'assetAllocationM',
  state: {
    timeData: [],
    selectData: [],
  },

  effects: {
    //  获取时间区间的可选时间范围
    *queryProductTime({ payload, callback }, { call, put }) {
      const res = yield call(api.assetAllocation.queryProductTime, payload);
      if (res && res.data && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            timeData: res,
          },
        });
        callback(res);
      } else {
        message.error(res.msg);
      }
    },
    //获取资产大类数据
    *queryAssetsBig({ payload, callback }, { call, put }) {
      const response = yield call(api.assetAllocation.queryAssetsData, payload);
      if (response && response.code == 0) {
        callback(response.data);
      } else {
        callback();
        message.error(response && response.msg ? response.msg : '查询资产大类数据失败!');
      }
    },

    //获取占比趋势图数据
    *queryTrendChart({ payload, callback }, { call, put }) {
      const response = yield call(api.assetAllocation.queryTendencyChartData, payload);
      if (response && response.code == 0) {
        callback(response.data);
      } else {
        callback();
        message.error(response && response.msg ? response.msg : '查询占比趋势图数据失败!');
      }
    },
    //获取持仓明细数据
    *queryMarkeData({ payload, callback }, { call, put }) {
      const response = yield call(api.assetAllocation.queryMarkeData, payload);
      if (response && response.code == 0) {
        if (response.msg == 'Success') {
          callback(response);
        } else {
          // 当穿透条件为是时，点击查询，判断当前产品是否有穿透持仓数据，如果没有，弹窗提示
          if (response.msg == 'Success该产品穿透基本信息不全') {
            message.warning('该产品穿透基本信息不全，当前展示非穿透结果');
            callback(response);
          } else {
            message.warning(response?.msg);
            callback();
          }
        }
      } else {
        callback();
        message.error(response && response.msg ? response.msg : '查询持仓明细数据失败!');
      }
    },
    //获取大类资产维度统计数据
    *queryDimensionData({ payload, callback }, { call, put }) {
      const response = yield call(api.assetAllocation.queryDimensionData, payload);
      if (response && response.code == 0) {
        callback(response.data);
      } else {
        callback();
        message.error(response && response.msg ? response.msg : '查询大类资产维度统计数据失败!');
      }
    },
    //产品分配
    *queryDistributionData({ payload, callback }, { call, put }) {
      const response = yield call(api.assetAllocation.queryDistributionData, payload);
      if (response && response.code == 0) {
        callback(response.data);
      } else {
        callback();
        message.error(response && response.msg ? response.msg : '查询大类资产维度统计数据失败!');
      }
    },
    //  获取时间区间的可选时间范围
    *findProductTime({ payload, callback }, { call, put }) {
      const res = yield call(api.profitAndLoss.findProductTime, payload);
      if (res && res.data && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            timeData: res,
          },
        });
        callback(res);
      } else {
        message.error(res?.msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
